package done

import (
	"fmt"
	"time"
)

func handle() {
	messages := make(chan int, 10)
	done := make(chan bool)
	defer close(messages)

	go consume(messages, done)
	produce(messages)

	time.Sleep(5 * time.Millisecond)
	done <- true
	time.Sleep(1 * time.Millisecond)
	fmt.Println("main process exit!")
}

func produce(messages chan int) {
	for i := 0; i < 10; i++ {
		messages <- i
	}
}

func consume(messages chan int, doneChan chan bool) {
	ticker := time.NewTicker(1 * time.Millisecond)
	for range ticker.C {
		select {
		case done := <-doneChan:
			if done {
				fmt.Println("child process interrupt...")
				return
			}
		default:
			fmt.Printf("send message: %d\n", <-messages)
		}
	}
}
